package ge.telasi.tasks.model.validation;

import ge.telasi.tasks.TaskException;
import ge.telasi.tasks.model.Task;
import ge.telasi.tasks.model.TaskReceiver;
import java.util.Calendar;
import java.util.List;

/**
 * @author dimitri
 */
public class TaskValidator extends Validator {

    public void validateFull(Task task) {
        validateSubjectAndBody(task);
        validateDueDate(task.getDueDate());
        validateAssignees(task.getAssignees());
    }

    public void validateSubjectAndBody(Task task) {
        validateSubject(task.getSubject());
        validateBody(task.getLastBody());
    }

    public void validateSubject(String subject) {
        String name = "Task.subject";
        String value = subject;
        new EmptyFieldValidator().validate(name, value);
        new TextRangeValidator(0, 255).validate(name, value);
    }

    public void validateBody(String body) {
        String name = "Task.body";
        String value = body;
        new EmptyFieldValidator().validate(name, value);
    }

    public void validateDueDate(Calendar dueDate) {
        if (dueDate != null) {
            String name = "Task.dueDate";
            Calendar now = Calendar.getInstance();
            now.set(Calendar.HOUR, 0);
            now.set(Calendar.MINUTE, 0);
            now.set(Calendar.SECOND, 0);
            now.set(Calendar.MILLISECOND, 0);
            if (dueDate.getTimeInMillis() < now.getTimeInMillis()) {
                String key = "TaskValidator.taskWithBackDate";
                String msg = getLocaleManager().getMessage(key);
                throw new TaskException(msg, name);
            }
        }
    }

    public void validateAssignees(List<TaskReceiver> assignees) {
        if (assignees == null || assignees.isEmpty()) {
            String key = "Task.defineTaskAssignees";
            String msg = getLocaleManager().getMessage(key);
            throw new TaskException(msg, "Task.assignees");
        }
    }

}
